package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.UserInfoDetailsVO;
import com.atguigu.srb.core.pojo.vo.UserInfoIndexVO;
import com.atguigu.srb.core.pojo.vo.UserLoginVO;
import com.atguigu.srb.core.pojo.vo.UserRegisterVO;
import com.atguigu.srb.core.service.UserInfoService;
import com.atguigu.srb.result.ResponseVo;
import com.atguigu.srb.util.JwtUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2024-04-15
 */
@RestController
@RequestMapping("/api/core/userInfo")
@Api(tags = "普通用户接口")
public class ApiUserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Resource
    private UserInfoMapper userInfoMapper;
    @ApiOperation("获取用户首页信息")
    @GetMapping("/index")
    public ResponseVo UserIndex(@RequestHeader(value = "token",required = false) String token){
        /*
        *   lastLoginTime   user_login_record
        *   headImg  user_info
        *   name   user_info
        *   userType  user_info
        *   bindStatus  user_info
        *   amount          user_account
        *   freezeAmount    user_account
        * */
        Long userId = JwtUtils.getUserId(token);
        UserInfoIndexVO userInfoIndexVO = userInfoMapper.selectUserIndexVOById(userId);
        return ResponseVo.ok().addData("item",userInfoIndexVO);
    }

    @ApiOperation("跟新用户的手机号和类型")
    @PutMapping("/mobileAndType")
    public ResponseVo updateMobileAndType(@RequestHeader("token") String token,
                                          @RequestBody UserLoginVO userLoginVO){
        userInfoService.updateMobileAndType(token,userLoginVO);
        return ResponseVo.ok().message("更新成功");
    }
    @ApiOperation("根据token判断是否绑定了手机号")
    @GetMapping("/isBindMobile")
    public ResponseVo isBindMobile(@RequestHeader("token") String token){
        Boolean flag = userInfoService.isBindMobile(token);
        return  ResponseVo.ok().addData("flag",flag);
    }
    @ApiOperation("判断手机号是否已经注册使用")
    @GetMapping("/isMobileUsed/{mobile}")
    public ResponseVo isMobileUsed(@PathVariable String  mobile) throws InterruptedException {
//        TimeUnit.SECONDS.sleep(5);
        int count = userInfoService.count(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getMobile, mobile));
        System.out.println("count = " + count);
        return ResponseVo.ok().addData("isMobileUsed",count>0);
    }
    @ApiOperation("用户注册")
    @PostMapping("register")
    public ResponseVo register(@RequestBody UserRegisterVO userRegisterVO){
        userInfoService.register(userRegisterVO);
        return ResponseVo.ok().message("注册成功");
    }
    @ApiOperation("用户登陆")
    @PostMapping("login")
    public ResponseVo login(@RequestBody UserLoginVO userLoginVO, HttpServletRequest request){
        String token = userInfoService.login(userLoginVO,request);
        return ResponseVo.ok().addData("token",token).message("登陆成功");
    }
    @ApiOperation("根据token，获取界面展示的用户信息")
    @GetMapping("details")
    public ResponseVo getDetailsById(@RequestHeader("token") String token){
        Long userId = JwtUtils.getUserId(token);
        UserInfo userInfo = userInfoService.getById(userId);
        UserInfoDetailsVO userInfoDetailsVO = new UserInfoDetailsVO();
        BeanUtils.copyProperties(userInfo,userInfoDetailsVO);
        return ResponseVo.ok().addData("item",userInfoDetailsVO);
    }
}

